-[ c library "type.c", header "type.h" ]

-[ headers, begin ]
	!stdlib !stdio
-[ end ]

-[public] typedef union type type_t;

-[public] enum type_kind
	{ type_basic, type_array,
	   type_pointer, type_record };

-[public] enum basic_kind 
	{ basic_int };

-[public] struct basic_type {
	enum type_kind kind;
	enum basic_kind basic;
};

-[public] struct pointer_type {
	enum type_kind kind;
	type_t *base;
};

-[public] struct array_type {
	enum type_kind kind;
	int size;
	type_t *base;
};

-[public] struct field {
	char *name;
	type_t *type;
};

-[ fieldlist_t : public list of struct field* ]

-[public] struct record_type {
	enum type_kind kind;
	int ident;
	fieldlist_t *fields;
};

-[public] union type {
	enum type_kind kind;
	struct basic_type basic;
	struct array_type array;
	struct pointer_type pointer;
	struct record_type record;
};


-[ public] void dump_type(type_t *T)
{
	switch(T->kind) {
	case type_basic: dump_basic_type(&T->basic); break;
	case type_pointer:
		printf(" pointer( ");
		dump_type(T->pointer.base);
		printf(" )");
		break;
	}
}

-[private] void dump_basic_type(struct basic_type* B)
{
	switch(B->basic) {
	case basic_int: printf("int "); break;
	}
}

-[public] int types_equal(type_t *A,type_t *B)
{
	if (A->kind!=B->kind) return 0;
	if (A->kind==type_basic) 
		return A->basic.basic==B->basic.basic;
	exit(fprintf(stderr,"unimlemented type in types_equal\n"));
	return 0;
}

-[public] int sizeof_type(type_t *T)
{
	switch(T->kind) {
	case type_basic:
		switch(T->basic.basic) {
			/* FIXME: host vs. target*/
		case basic_int: return sizeof(int);
		}
		break;
	}
	exit(fprintf(stderr,"unimlemented type in sizeof_type\n"));
	return 0;
}

